/**
 * All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectVisitExpertController
 * 评分细则专家拜访
 *
 * 修改纪录
 * 2022-11-14 版本：1.0 houyuhang 创建。
 * @version 版本：1.0
 * @author 作者：houyuhang
 * 创建日期：2022-11-14
 */

package com.topscomm.mpm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollectionUtil;
import com.topscomm.basic.BasicEntity;
import com.topscomm.mpm.pojo.MpmProjectVisitExpertEntity;
import com.topscomm.mpm.service.impl.MpmProjectVisitExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.mpm.controller.auto.MpmProjectVisitExpertControllerAuto;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst.TapOperationTypeEnum;

/**
 * @author: houyuhang
 * @date: 2022-11-14
 * @description:评分细则专家拜访Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectVisitExpert")
public class MpmProjectVisitExpertController extends MpmProjectVisitExpertControllerAuto implements IMpmProjectVisitExpertController {
	@Autowired
	private MpmProjectVisitExpertService mpmProjectVisitExpertService;
	/**
	 * @author: houyuhang
	 * @date: 2022-11-14
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled=1 ");
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_docno")) {
			String value = paraMap.get("qry_docno").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docno like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_begindocdate")) {
			String value = paraMap.get("qry_begindocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate >= {0} ", SQLAdapter.toDateLong(value + " 00:00:00"));
		}
		if (paraMap.containsKey("qry_enddocdate")) {
			String value = paraMap.get("qry_enddocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate <= {0}", SQLAdapter.toDateLong(value + " 23:59:59"));
		}
		if (paraMap.containsKey("qry_deptname")) {
			String value = paraMap.get("qry_deptname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (deptid in ( SELECT id from CboDept where fullname like '%{0}%'))", value);
		}
		if (paraMap.containsKey("qry_username")) {
			String value = paraMap.get("qry_username").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (username like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_deptcode")) {
			String value = paraMap.get("qry_deptcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcode like '{0}%'", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_bidscoretimes")) {
			String value = paraMap.get("qry_bidscoretimes").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and bidscoretimes={0}", value);
		}
		if (paraMap.containsKey("qry_where")) {
			String value = paraMap.get("qry_where").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}", value);
		}
		if (this.getUserBean().getUser().getAdminlevel() < 1)// 管理员、超级管理员，不控制权限
		{
			String dataPession = service.getDataPermission(TapOperationTypeEnum.Select, "", this.getUserBean());
			if (StringUtil.isEmpty(dataPession))// 默认权限只能看本人，部门负责人为自己的部门和下级部门的数据
			{
				String deptcodes_header = ConvertUtil.convertToString(this.getUserBean().getUser().getValue("deptcodes_header"));
				if (StringUtil.isEmpty(deptcodes_header))
					sql.appendSingle(" and (usercode = '{0}' or deptcode ='{1}' or deptcode = '{2}' )", this.getUserBean().getUser().getCode(), this.getUserBean().getDept().getCode(), this
							.getUserBean().getWorkDept().getCode());
				else
					sql.appendSingle(" and (deptcode in ({0})  or usercode = '{1}' or deptcode ='{2}' or deptcode = '{3}' )", deptcodes_header, this.getUserBean().getUser().getCode(), this
							.getUserBean().getDept().getCode(), this.getUserBean().getWorkDept().getCode());
			} else// 使用数据权限
			{
				sql.appendSingle(" and {0} ", dataPession);
			}
		}
		return sql.toString();
	}

	/**
	 * @author: houyuhang
	 * @date: 2022-11-14
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 *
	 * @description: 提交审批前
	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author: houyuhang
	 * @date: 2022-11-14
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}{1}于{2}提交评标专家拜访反馈申请,单据编号{3}! ", dataMap.get("dispdeptcode"), dataMap.get("username"), DateUtil.dateToString(new Date(), "yyyy-MM-dd"), dataMap.get("docno"));
		dataMap.put("maintitle", mailtitle.toString());
	}

	/**
	 * @author: houyuhang
	 * @date: 2022-11-15
	 * @param paraMap
	 * @return
	 * @description:获取评标专家拜访反馈信息
	 * @modify:
	 */
	@GetMapping(value = "/queryVisitExpertList")
	public ResponseResult<Object> queryVisitExpertList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> res = mpmProjectVisitExpertService.queryVisitExpertList(paraMap);
			return ResponseResult.ok(res);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-12-02
	 * @param paraMap
	 * @return
	 * @description:保存评标专家拜访反馈
	 * @modify:
	 */
	@PostMapping(value = "/saveVisitExpert")
	public ResponseResult<Object> saveVisitExpert(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			mpmProjectVisitExpertService.saveVisitExpert(paraMap);
			return ResponseResult.ok("保存成功！");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-12-05
	 * @param paraMap
	 * @return
	 * @description:审批页面，保存评标专家拜访反馈及评论内容
	 * @modify:
	 */
	@PutMapping(value = "/saveVisitExpertApproval")
	public ResponseResult<Object> saveVisitExpertApproval(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			mpmProjectVisitExpertService.saveVisitExpertApproval(paraMap);
			return ResponseResult.ok("保存成功！");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
	/**
	 *
	 * @description: 提交审批后将所有此次审批单据的状态置为1
 	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author: houyuhang
	 * @date: 2022-12-05
	 * @modify:
	 */
	@Override
	protected void afterSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.afterSubmit(dataMap, paraMap);
		long id = ConvertUtil.convertToLong(paraMap.get("id"));
		String projectcode = ConvertUtil.convertToString(paraMap.get("projectcode"));
		String usercode = ConvertUtil.convertToString(paraMap.get("usercode"));
		Date submitdate = ConvertUtil.convertToDate(paraMap.get("submitdate"));
		String bidScoreTimes =  ConvertUtil.convertToString(paraMap.get("bidscoretimes"));
		// 查询该项目本人开立、退回的单据，置为审核中
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" projectcode = '{0}' and state in (0,2) and usercode = '{1}' and bidscoretimes = '{2}' ", projectcode, usercode, bidScoreTimes);
		List<MpmProjectVisitExpertEntity> mpmProjectVisitExpertEntities = this.service.queryByWhere(sql.toString());
		List<BasicEntity> basicEntities = new ArrayList<>();
		if (!CollectionUtil.isEmpty(mpmProjectVisitExpertEntities)) {
			for (MpmProjectVisitExpertEntity mpmProjectVisitExpertEntity : mpmProjectVisitExpertEntities) {
				mpmProjectVisitExpertEntity.setState(1);
				mpmProjectVisitExpertEntity.setRelateid(id);
				mpmProjectVisitExpertEntity.setSubmitdate(submitdate);
				mpmProjectVisitExpertEntity.setLatestphase("");
				basicEntities.add(mpmProjectVisitExpertEntity);
			}
		}
		this.service.updateBatch(basicEntities);
		// 审批主单更新relateid
		sql.clear();
		sql.appendSingle(" update mpmprojectvisitexpert set relateid = '{0}' where id = '{0}' ", id);
		this.service.updateExecute(sql.toString());
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-12-05
	 * @param paraMap
	 * @return
	 * @description:获取评标专家拜访反馈审批信息
	 * @modify:
	 */
	@GetMapping(value = "/queryApprovalList")
	public ResponseResult<Object> queryApprovalList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> res = mpmProjectVisitExpertService.queryApprovalList(paraMap);
			return ResponseResult.ok(res);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
}

